/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.DocumentDAO;
import exatrez.dao.FolderDAO;
import exatrez.dao.PersonalTaskDAO;
import exatrez.dao.TaskFileDAO;
import exatrez.dao.TaskSummaryDAO;
import exatrez.model.Document;
import exatrez.model.Folder;
import exatrez.model.PersonalTask;
import exatrez.model.TaskFile;
import exatrez.model.TaskSummary;
import exatrez.model.User;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
public class TaskManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private PersonalTaskDAO personalTaskDAO;
    @Inject
    private TaskSummaryDAO taskSummaryDAO;
    private static Logger LOG = LoggerFactory.getLogger(TaskManager.class);
    @Inject
    private TaskFileDAO taskFileDAO;
    @Inject
    private DocumentDAO documentDAO;
    @Inject
    private FolderDAO folderDAO;

    @Transactional
    public void saveOrUpdate(PersonalTask personalTask) {
        personalTaskDAO.saveOrUpdate(personalTask);
    }

    @Transactional
    public void saveOrUpdateSummary(TaskSummary taskSummary) {
        taskSummaryDAO.saveOrUpdate(taskSummary);
    }

    @Transactional
    public void saveOrUpdateTaskFile(TaskFile taskFile) {
        taskFileDAO.saveOrUpdate(taskFile);
    }

    @Transactional
    public void deleteFile(TaskFile taskFile) {
        taskFileDAO.delete(taskFile);
    }

    @Transactional
    public void saveOrUpdateDocument(Document document) {
        documentDAO.saveOrUpdate(document);
    }

    @Transactional
    public void saveOrUpdateDocument(List<Document> documents) {
        for (Document document : documents) {
            documentDAO.saveOrUpdate(document);
        }
    }

    @Transactional
    public void saveOrUpdateFolder(Folder folder) {
        folderDAO.saveOrUpdate(folder);
    }

    public List<PersonalTask> findAll() {
        String query = "SELECT a FROM PersonalTask a WHERE a.personalTaskId is not null and a.statusRemarks = 'Open' order by a.infoAudit.createDate asc";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public List<PersonalTask> findTaskUser(User user) {
        String query = "SELECT a FROM PersonalTask a WHERE a.assignee.userId = :userId and a.statusRemarks = 'Open' order by a.infoAudit.createDate asc";
        Query q = sessionProvider.get().createQuery(query).setString("userId", user.getUserId());
        return q.list();
    }

    public List<PersonalTask> findTaskUserClose(User user) {
        String query = "SELECT a FROM PersonalTask a WHERE a.assignee.userId = :userId and a.statusRemarks = 'Close' order by a.infoAudit.createDate asc";
        Query q = sessionProvider.get().createQuery(query).setString("userId", user.getUserId());
        return q.list();
    }

    public List<PersonalTask> findTaskCooClose() {
        String query = "SELECT a FROM PersonalTask a WHERE a.personalTaskId is not null and a.statusRemarks = 'Close' order by a.infoAudit.createDate asc";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public List<TaskSummary> findSummaryTask(String personalTaskId) {
        String query = "SELECT a FROM TaskSummary a WHERE a.personalTaskId = :personalTaskId  order by a.dateSubmit asc";
        Query q = sessionProvider.get().createQuery(query).setString("personalTaskId", personalTaskId);
        return q.list();
    }

    public List<TaskFile> findAllfile(String userId) {
        String query = "SELECT a FROM TaskFile a WHERE a.userId = :userId";
        Query q = sessionProvider.get().createQuery(query).setString("userId", userId);
        return q.list();
    }

    public List<PersonalTask> findTaskBySubject(PersonalTask personalTask, User user) {
        StringBuilder sb = new StringBuilder("select p from PersonalTask p ")
                .append("where personalTaskId is not null and statusRemarks = 'Open' and assignee.userId = :userId ");

        Session session = sessionProvider.get();

        if (personalTask != null && personalTask.getSubject() != null) {
            sb.append("and subject like :subject ");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            sb.append("and date(infoAudit.createDate) = :date ");
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            sb.append("and priorityCode.priorityCodeId = :priorityCodeId ");
        }
        Query query = session.createQuery(sb.toString()).setParameter("userId", user.getUserId());

        if (personalTask != null && personalTask.getSubject() != null) {
            query.setParameter("subject", "%" + personalTask.getSubject() + "%");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            query.setParameter("date", personalTask.getInfoAudit().getCreateDate());
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            query.setParameter("priorityCodeId", personalTask.getPriorityCode().getPriorityCodeId());
        }

        return query.list();
    }

    public List<PersonalTask> findTaskBySubjectClose(PersonalTask personalTask, User user) {
        StringBuilder sb = new StringBuilder("select p from PersonalTask p ")
                .append("where personalTaskId is not null and statusRemarks = 'Close' and assignee.userId = :userId ");

        Session session = sessionProvider.get();

        if (personalTask != null && personalTask.getSubject() != null) {
            sb.append("and subject like :subject ");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            sb.append("and date(infoAudit.createDate) = :date ");
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            sb.append("and priorityCode.priorityCodeId = :priorityCodeId ");
        }
        Query query = session.createQuery(sb.toString()).setParameter("userId", user.getUserId());

        if (personalTask != null && personalTask.getSubject() != null) {
            query.setParameter("subject", "%" + personalTask.getSubject() + "%");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            query.setParameter("date", personalTask.getInfoAudit().getCreateDate());
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            query.setParameter("priorityCodeId", personalTask.getPriorityCode().getPriorityCodeId());
        }

        return query.list();
    }

    public List<PersonalTask> findTaskBySubjectCoo(PersonalTask personalTask) {
        StringBuilder sb = new StringBuilder("select p from PersonalTask p ")
                .append("where personalTaskId is not null and statusRemarks = 'Open' ");

        Session session = sessionProvider.get();

        if (personalTask != null && personalTask.getSubject() != null) {
            sb.append("and subject like :subject ");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            sb.append("and date(infoAudit.createDate) = :date ");
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            sb.append("and priorityCode.priorityCodeId = :priorityCodeId ");
        }
        Query query = session.createQuery(sb.toString());

        if (personalTask != null && personalTask.getSubject() != null) {
            query.setParameter("subject", "%" + personalTask.getSubject() + "%");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            query.setParameter("date", personalTask.getInfoAudit().getCreateDate());
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            query.setParameter("priorityCodeId", personalTask.getPriorityCode().getPriorityCodeId());
        }

        return query.list();
    }

    public List<PersonalTask> findListTaskCloseCoo(PersonalTask personalTask) {
        StringBuilder sb = new StringBuilder("select p from PersonalTask p ")
                .append("where personalTaskId is not null and statusRemarks = 'Close' ");

        Session session = sessionProvider.get();

        if (personalTask != null && personalTask.getSubject() != null) {
            sb.append("and subject like :subject ");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            sb.append("and date(infoAudit.createDate) = :date ");
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            sb.append("and priorityCode.priorityCodeId = :priorityCodeId ");
        }
        Query query = session.createQuery(sb.toString());

        if (personalTask != null && personalTask.getSubject() != null) {
            query.setParameter("subject", "%" + personalTask.getSubject() + "%");
        }

        if (personalTask != null && personalTask.getInfoAudit() != null) {
            query.setParameter("date", personalTask.getInfoAudit().getCreateDate());
        }

        if (personalTask != null && personalTask.getPriorityCode() != null) {
            query.setParameter("priorityCodeId", personalTask.getPriorityCode().getPriorityCodeId());
        }

        return query.list();
    }

    public PersonalTaskDAO getPersonalTaskDAO() {
        return personalTaskDAO;
    }

    public void setPersonalTaskDAO(PersonalTaskDAO personalTaskDAO) {
        this.personalTaskDAO = personalTaskDAO;
    }

    public TaskSummaryDAO getTaskSummaryDAO() {
        return taskSummaryDAO;
    }

    public void setTaskSummaryDAO(TaskSummaryDAO taskSummaryDAO) {
        this.taskSummaryDAO = taskSummaryDAO;
    }

    public TaskFileDAO getTaskFileDAO() {
        return taskFileDAO;
    }

    public void setTaskFileDAO(TaskFileDAO taskFileDAO) {
        this.taskFileDAO = taskFileDAO;
    }

    public DocumentDAO getDocumentDAO() {
        return documentDAO;
    }

    public void setDocumentDAO(DocumentDAO documentDAO) {
        this.documentDAO = documentDAO;
    }

    public FolderDAO getFolderDAO() {
        return folderDAO;
    }

    public void setFolderDAO(FolderDAO folderDAO) {
        this.folderDAO = folderDAO;
    }
}
